package cn.mulanbay.gateway.config;

import cn.mulanbay.business.config.CacheConfig;
import cn.mulanbay.business.handler.HandlerManager;
import cn.mulanbay.business.handler.PasswordHandler;
import cn.mulanbay.business.handler.ThreadPoolHandler;
import cn.mulanbay.business.processor.ConfigPropertiesProcessor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.stream.Collectors;

/**
 * SpringMVC相关配置
 *
 * @author fenghong
 * @create 2022-01-16 10:00
 */
@Configuration
public class WebConfig {

    /**
     * 解决value设置默认值后无法注入问题
     * @return
     */
    @Primary
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer;
    }

    /**
     * 资源文件管理，支持国际化
     *
     * @return
     */
    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("utf-8");
        messageSource.setCacheSeconds(120);
        return messageSource;
    }

    /**
     * 配置文件处理
     * @return
     */
    @Bean
    public ConfigPropertiesProcessor configPropertiesProcessor() {
        ConfigPropertiesProcessor pp = new ConfigPropertiesProcessor();
        pp.setIgnoreUnresolvablePlaceholders(true);
        pp.setIgnoreResourceNotFound(true);
        //todo
        pp.setLocations();
        pp.setFileEncoding("utf-8");
        return pp;
    }

    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }

    /**
     * 线程池处理
     * @return
     */
    @Bean
    public ThreadPoolHandler threadPoolHandler() {
        return new ThreadPoolHandler();
    }

    /**
     * 密码处理
     * @return
     */
    @Bean
    public PasswordHandler passwordHandler() {
        return new PasswordHandler();
    }

    /**
     * 处理器管理类
     * @return
     */
    @Bean
    public HandlerManager handlerManager() {
        return new HandlerManager();
    }

    /**
     * 缓存配置
     */
    @Configuration
    public class MyCacheConfig extends CacheConfig {

    }
}
